package br.usjt.lab.hirose;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class FillFlood extends Canvas implements Runnable {
	
	private static final long serialVersionUID = 1L;
	public static int LARGURA = 300;
	public static int ALTURA = LARGURA / 16 * 9;

	public static int ESCALA = 3;
	public static final String TITLE = "Flood Fill";

	private static JButton botao = new JButton("Pintar");

	private boolean isRunning;
	private Thread thread;
	
	private Render render;
	private BufferedImage img = new BufferedImage(LARGURA, ALTURA, BufferedImage.TYPE_INT_RGB);
	private int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
	public static theHandler listener;
	
	public FillFlood() {
		Dimension size = new Dimension(LARGURA * ESCALA, ALTURA * ESCALA);
		setPreferredSize(size);
		listener = new theHandler();
		
		render = new Render(LARGURA, ALTURA);
	}

	public synchronized void start() {
		isRunning = true;
		thread = new Thread(this, "Flood Fill");
		thread.start();
	}

	public synchronized void stop() {
		isRunning = false;
		try{
			thread.join();
		}
		catch(InterruptedException e){
			e.printStackTrace();
		}
	}

	public void run() {
		while (isRunning) {
			render();
		}
	}

	public void render() {
		BufferStrategy bs = getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(3);
			return;
		}

		Graphics g = bs.getDrawGraphics();
		g.setColor(Color.BLACK);

		g.fillRect(0, 0, getWidth(), getHeight());

		g.dispose();
		bs.show();
	}

	public static void main(String[] args) {
		FillFlood ff = new FillFlood();
		JFrame frame = new JFrame();
		JPanel painel = new JPanel();
		
		painel.add(ff, BorderLayout.NORTH);
		painel.add(botao, BorderLayout.SOUTH);
		
		frame.add(painel, BorderLayout.NORTH);
		
		botao.addActionListener(listener);
		frame.pack();
		frame.setTitle(TITLE);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		
		ff.start();
	}
	
	 private class theHandler implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource() == botao){
				System.out.println("Botao");
			}
		}
		
	}
}
